package ca.grocerygo.android.utils; import android.util.Log; import com.google.gson.JsonArray; import com.google.gson.JsonParser; import com.google.gson.stream.JsonReader; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import java.io.*; /** * User: robert * Date: 06/02/13 */ public class JSONParser { static InputStream is = null; static JsonArray jObj = null; static String json = ""; // constructor public JSONParser() { } public JsonReader getReaderFromUrl(String url) { Reader reader; InputStream inputStream = null; // Making HTTP request try { // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); inputStream = httpEntity.getContent(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } JsonReader jsonReader = new JsonReader(new InputStreamReader(inputStream)); jsonReader.setLenient(true); return jsonReader; } public JsonArray getJSONFromUrl(String url) { // Making HTTP request try { // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { BufferedReader reader = new BufferedReader(new InputStreamReader( is, "UTF-8"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); json = sb.toString(); } catch (Exception e) { Log.e("Buffer Error", "Error converting result " + e.toString()); } // try parse the string to a JsonArray JsonParser jsonParser = new JsonParser(); jObj = jsonParser.parse(json).getAsJsonArray(); return jObj; } }